package config

import (
	"database/sql"
	"sort"
	"time"
)

const (
	RedisActivityId    = "redPack:activityId"
	RedisRedPackAmount = "redPack:redPackAmount"
	RedisRedPackCount  = "redPack:redPackCount"
)

type RedPackCfg struct {
	BeginDay       int //开始日期时间戳'
	EndDay         int //
	DailyBeginHour int //开始日期内开始小时
	DailyEndHour   int //
	RedPackNum     int //红包池 红包数量

	RedPackTotalMoney int //红包池 总金额
	CheckType         int //0注册用户 1当天充值用户 2历史充值用户 3vip 用户
	GetMaxCount       int //该 x小时内一玩家最多领几个
	ActivityId        int //活动id
	DailyGetOne       int //一人一天领一个

	OnOff    int     //开关
	MoneyMin float64 //最小金额
	MoneyMax float64 //最大金额
}

// 活动(或下次活动)开启的时间
type RedPackActTime struct {
	ActivityId int //活动id
	StartTime  int64
}

type RedPackMgr struct{}

var G_RedPackCfgList []RedPackCfg
var G_RedPackMgr RedPackMgr

func RedPackCfgSetRow(rows *sql.Rows) {
	var tVal RedPackCfg
	var err error
	err = rows.Scan(&tVal.BeginDay, &tVal.EndDay, &tVal.DailyBeginHour, &tVal.DailyEndHour, &tVal.RedPackNum,
		&tVal.RedPackTotalMoney, &tVal.CheckType, &tVal.GetMaxCount, &tVal.ActivityId, &tVal.DailyGetOne,
		&tVal.OnOff, &tVal.MoneyMin, &tVal.MoneyMax)
	if err != nil {
		return
	}
	G_RedPackCfgList = append(G_RedPackCfgList, tVal)
}

func RedPackCfgRemove() {
	G_RedPackCfgList = []RedPackCfg{}
}

func (that *RedPackMgr) GetCurActivity() RedPackCfg {

	now := time.Now()
	nowHour := now.Hour()
	nowStamp := now.Unix()

	for _, val := range G_RedPackCfgList {
		if int64(val.BeginDay) < nowStamp || int64(val.EndDay) >= nowStamp {
			continue
		}

		if val.DailyBeginHour < nowHour || val.DailyEndHour >= nowHour {
			continue
		}

		return val

	}

	return RedPackCfg{}
}

func (that *RedPackMgr) IsActivityTime() bool {
	actInfo := that.GetCurActivity()
	if 0 == actInfo.ActivityId {
		return true
	}

	return false
}

// 目前不在活动中，获取下次活动的开始时间
func (that *RedPackMgr) GetNextActTime() int64 {

	now := time.Now()
	nowHour := now.Hour()
	nowStamp := now.Unix()

	IsActOpenFunc := func(nowStamp int64, actCfg RedPackCfg) bool {
		if int64(actCfg.BeginDay) < nowStamp || int64(actCfg.EndDay) >= nowStamp {
			return false
		}
		return true
	}

	redPackActTimeList := []RedPackActTime{}
	for _, val := range G_RedPackCfgList {
		if !IsActOpenFunc(nowStamp, val) {
			continue
		}

		if val.DailyBeginHour < nowHour || val.DailyEndHour >= nowHour {
			continue
		}

		var actTime int64

		actTime = time.Date(now.Year(), now.Month(), now.Day(), val.DailyBeginHour, 0, 0, 0, now.Location()).Unix()

		//明天的活动
		if val.DailyBeginHour <= nowHour {
			actTime += 24 * 60 * 60
		}

		if !IsActOpenFunc(actTime, val) {
			continue
		}

		redPackActTimeList = append(redPackActTimeList, RedPackActTime{ActivityId: val.ActivityId, StartTime: actTime})
	}

	if 0 == len(redPackActTimeList) {
		return 0
	}

	sort.Slice(redPackActTimeList, func(i, j int) bool {
		return redPackActTimeList[i].StartTime < redPackActTimeList[j].StartTime
	})

	return redPackActTimeList[0].StartTime
}

// 传入的时间在当前活动中
func (that *RedPackMgr) TimeInCurActivity(compareTime int64) bool {
	actInfo := that.GetCurActivity()
	if 0 == actInfo.ActivityId {
		return false
	}

	now := time.Now()

	actBeginTime := time.Date(now.Year(), now.Month(), now.Day(), actInfo.DailyBeginHour, 0, 0, 0, now.Location()).Unix()
	actEndTime := time.Date(now.Year(), now.Month(), now.Day(), actInfo.DailyEndHour, 0, 0, 0, now.Location()).Unix()

	if compareTime > actEndTime || compareTime < actBeginTime {
		return false
	}

	return true
}
